package com.mecuros.handler.deduplication;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.google.common.collect.Lists;
import com.mecuros.common.constant.AustinConstant;
import com.mecuros.common.domain.TaskInfo;
import com.mecuros.common.enums.DeduplicationType;
import com.mecuros.handler.domain.DeduplicationParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname DeduplicationRuleService
 * @Description
 * @Date 2022/5/17 9:22 下午
 * @Created by yujianbo
 */
@Service
public class DeduplicationRuleService {

    public static final String DEDUPLICATION_RULE_KEY = "deduplication";

    @ApolloConfig("boss.austin")
    private Config config;

    @Autowired
    private DeduplicationHolder deduplicationHolder;


    public void duplication(TaskInfo taskInfo){
        // 配置样例：{"contentDeduplication":{"num":1,"time":300},"frequencyDeduplication":{"num":5}}
        String deduplicationConfig = config.getProperty(DEDUPLICATION_RULE_KEY, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_OBJECT);

        List<Integer> deduplicationList = DeduplicationType.getDeduplicationList();
        // 去重
        for (Integer deduplicationType : deduplicationList) {
            DeduplicationParam deduplicationParam = deduplicationHolder.selectBuilder(deduplicationType).build(deduplicationConfig, taskInfo);
            if (deduplicationParam != null){
                deduplicationHolder.selectService(deduplicationType).deduplication(deduplicationParam);
            }
        }
    }
}
